# -*- coding: utf-8 -*-

class eroad_bracelet_users(osv.osv):
    _name = 'eroad.barcelet.users'
    _description = u"易路手环用户列表"
    _columns = {
        'eroad_bracelet_id' : fields.many2one('eroad.bracelet',string=u'对应平台'),
        'active':fields.boolean(u'用户是否删除'),
    }
    
    _defaults = {
        'refresh_id_ok': false,
        'active':True
    }
    
    def get_user_info_url_codoon(self,user):
        url = ''
        if user.eroad_bracelet_id.is_official:
            #判断是否正式生产环境中使用
            url_root = user.eroad_bracelet_id.official_url
        else:
            url_root = user.eroad_bracelet_id.test_url
        params = {
            'uid':user.uid
            'access_token':user.ccess_token
        }
        uri = '/api/vertify_credentials'
        url = url_root + uri
        
        return url
        
    def get_user_info_codoon(self, user):
        status = 0
        ret = {}
        
        url = self.get_user_info_url_codoon(user)
        headers = {'Authorization':'Bearer %s' user.acccess_token}
        code, res = eroad_utils.http_request(url,{},headers)
        if code in [200,'200']:
            status = 1
        ret =res
        
        return status, ret
        
        
    def user_info_save_codoon(self,cr,uid,user,user_info):
        ret = 0
        
        value = user_info.copy()
        if value:
            val={
                'eroad_bracelet_user_id':user.id,
                'codoon_id':value.get('id',''),
                'domain':value.get('domain','')
                'certificationname':value.get('certificationname',''),
                'get_icon_large':value.get('get_icon_large':''),
                'get_icon_small':value.get('get_icon_small':''),
                'get_icon_tiny':value.get('get_icon_tiny':''),
                'get_icon_xlarge':value.get('get_icon_xlarge',''),
                'mobile_portraits_l':value.get('mobile_portraits_l')[0] if value.get('mobile_portraits_l') else '',
                'mobile_portraits_x':value.get('mobile_portraits_x')[0] if value.get('mobile_portraits_x') else '',
                'nick':value.get('nick':''),
                'followings':value.get('followings',0),
                'followers':value.get('followers',0),
                'location':value.get('location',''),
                'weight':value.get('weight',0),
                'height':value.get('height',0),
                'realname':value.get('realname',''),
                'address':value.get('adress',''),
                'gender':int(value.get('gender',0)),
                'gender_desc':'女' if int(value.get('gender')) ==0 else '男',
                'hobby':value.get('hobby':''),
                'description':value.get('description':''),
                'routes_count’:value.get('routes_count',''),
                'week_goal_type':value.get('week_goal_type':''),
                'week_goal_value':value.get('week_goal_value':0),
            }
            
            if value.get('birthday'):
                btd = value.get('birthday')
                vals['birthday'] = '%d-%d-%d' %(btd['y'],thd['m'],btd['d'])
            
            user_codoon_obj = self.pool['eroad.bracelet.user.codoon']
            user_codoon_ids = user_codoon_obj.search(cr,SUPERUSER_ID, [('eroad_bracelet_user_id','=',user.id)])
            
            if user_codoon_ids:
                ret = user_codoon_obj.write(cr,SUPERUSER_ID, users[0],vals,context{})
            else:
                ret = user_codoon_obj.create(cr,SUPPERUSER_ID,vals)
            
            #更新对应患者字段
            patient_obj = self.pool['eroad.bracelet.patient']
            patient_obj.write(cr,SUPERUSER_ID, user.eroad_bracelet_patient_id.id,{'weight':value.get('weight',0),'height':value.get('height',0)})
        return ret
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        